package com.appbee.core.client.event;

import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.ValueBoxBase;

/**
 * Filter out keyUp events if the value has not changed.
 * 
 * @author jakobmarrunarsson
 * 
 * @param <T>
 */
public abstract class KeyUpValueChangeHandler<T>
{
	private T currentValue;

	public KeyUpValueChangeHandler(final ValueBoxBase<T> valueBox)
	{
		this.currentValue = valueBox.getValue();
		valueBox.addKeyUpHandler(new KeyUpHandler()
		{

			@Override
			public void onKeyUp(KeyUpEvent event)
			{
				if (!currentValue.equals(valueBox.getValue()))
				{
					currentValue = valueBox.getValue();
					onKeyUpValueChange(event);
				}
			}
		});
	}

	public abstract void onKeyUpValueChange(KeyUpEvent event);

}
